/**
 * 
 * @returns
 */

/*获得当前项目名*/
function getRootPath(){
		//获取当前网址，
		var curWwwPath=window.document.location.href;
		//获取主机地址之后的目录
		var pathName=window.document.location.pathname;
		var pos=curWwwPath.indexOf(pathName);
		//获取主机地址，
		var localhostPaht=curWwwPath.substring(0,pos);
		//获取带"/"的项目名，
		var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
		return(projectName);
}

$(document).ready(function() {
		
		var menulist_left = $("#side-menu");
		
		var pathname = location.pathname;
		
		$.ajax({
			url : getRootPath()+'/admin/getmenus',
			type : 'post',
			async:false,
			dataType : 'json',
			success : function(data) {
				
				for(var i= 0;i<data.length;i++){
					
					var url = data[i].url ;
					var menuname = data[i].menuName;
					var childmenu = data[i].childmenu;
					var isstair = data[i].isstair;
					
					if(isstair == 1 && (url == null || url == '')){
						
						var onemenu ="<li><a href='#' class='links'><i class='fa fa-unlink'></i> <span class='nav-label'>"+menuname+"</span><span class='fa arrow'></span></a></li>" 
						
						var chileul = "<ul class='nav nav-second-level'></ul>"
						
						for(var j=0;j<childmenu.length;j++){
							chileul = $(chileul).append("<li><a class='J_menuItem' href='"+childmenu[j].url+"'>"+ childmenu[j].menuName +"</a></li>");
						}
						onemenu = $(onemenu).append(chileul);
						menulist_left.append(onemenu);
						
					}else{
						menulist_left.append("<li><a class='J_menuItem' href='"+url+"'><i class='fa fa-home'></i> <span class='nav-label'>"+menuname+"</span></a></li>")
					}
				}
				
			},
			error : function() {
				
			}
		});
		
		
});	
//
var file = null;
var uploader = null;
//修改头像
function updatephoto(){
	var user = null;
	user = getLoggedinuser();
	var str = `<div class="container" style="width:300px; height:300px;">
					<div class="row clearfix">
						<div class="col-md-12 column">
							<div class="row clearfix">
								<div class="col-md-5 column">
										<img alt="140x140" src="`+user.photo+`" class="img-circle" style="width:60px; height:60px; margin:20px;" id="preview"/>
										<h3 class="text-info">
											图片预览
										</h3>
								</div>
								<div class="col-md-7 column">
									<div id="uploader-demo">
									    <!--用来存放item-->
									    <div id="fileList" class="uploader-list"></div>
									    <div id="filePicker">选择图片</div>
									</div>
									
								</div>
							</div>
						</div>
					</div>
				</div>`
					//弹出一个层			
					layer.open({
						  type: 1,
						  title:"头像修改",
						  content:str, //这里content是一个普通的String
						  area :['300px','342px'],
						  shadeClose :true,
						  anim :0,
						  success:function (){
							 console.log("layer 弹出回调");
							 //创建上传对象
							 uploader = WebUploader.create({
								    // 选完文件后，是否自动上传。
								    auto: false,
								    // swf文件路径
								    swf: BASE_URL + '/js/Uploader.swf',
								    // 文件接收服务端。
								    server: getRootPath()+'/uploadBlogerphoto',
								    // 选择文件的按钮。可选。
								    // 内部根据当前运行是创建，可能是input元素，也可能是flash.
								    pick: '#filePicker',
								    // 只允许选择图片文件。
								    accept: {
								        title: 'Images',
								        extensions: 'gif,jpg,jpeg,bmp,png',
								        mimeTypes: 'image/*'
								    },
								    //上传方式
								    method :'post',
								    fileNumLimit :2,
								    fileVal :'file'
								});
							 
							  //当一个文件选完加入到文件队列后
							  uploader.on( 'fileQueued', function( newfile ) {
								  
								  if(file == null){
									  file = newfile;
									 var fileList = $('#fileList');
									 //添加上传按钮
									 var beginbutton = `<button type="button" onclick="photoupload()" class="btn btn-info">上传</button>`
									  fileList.parent().append(beginbutton);
									  
								    }else{
								    	uploader.removeFile( file.id,true );
								    	file = newfile;
								    }
								    // 创建缩略图
								    // 如果为非图片文件，可以不用调用此方法。
								    // thumbnailWidth x thumbnailHeight 为 100 x 100
								    uploader.makeThumb( newfile, function( error, src ) {
								        if ( error ) {
								             return;
								        }
								        $('#preview').attr( 'src', src );
								        
								    }, 100, 100 );
								});
							  
								//文件上传成功后
								uploader.on('uploadSuccess',function(callbackfile, response ){
									var status = response.status;
									var message = response.message;
									if(status == 2000){
										
										//获得前台显示头像的标签
										var userphotoobj = $('#userphoto');
										//获得后台返回插入好的后台头像
										var photo = response.photo;
										alert(message);
										//前台无刷新更换头像
										userphotoobj.attr('src',photo);
										layer.close(layer.index);
									}else{
										alert(message);
									}
								})
								
						  },
						  end:function (){
							 file = null;
							 uploader.destroy();
							 uploader = null;
							 
						  }
						});
	
}

//获得正在登陆的用户
function getLoggedinuser(){
	var user = {
			username:null,
			photo:null
		}
	$.ajax({
		url : getRootPath()+'/getLoggedinuser',
		type : 'get',
		async:false,
		dataType : 'json',
		success : function(resultdata) {
			if(resultdata.status == 2000){
				var  obj = resultdata.resultobj;
				user.username = obj.username;
				user.photo = obj.photo;
			}
		},
		error : function() {
			layer.msg('系统出错！！', {
		        icon : 2
		      });
		}
	});
	
	
	return user;
}

function photoupload(){
	uploader.upload(file);
}
